package com.wythe.mall.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.wythe.mall.R;
import com.wythe.mall.adapter.DeliveryStoreAdapter;
import com.wythe.mall.adapter.EvaluateGoodsAdapter;
import com.wythe.mall.adapter.EvaluateGoodsListAdapter;
import com.wythe.mall.adapter.ImageShopAdapter;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.RecommentBean;
import com.wythe.mall.model.ShopBean;
import com.wythe.mall.model.SizeBean;
import com.wythe.mall.utils.ApiOkHttp;
import com.wythe.mall.view.NoScrollGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import top.yokey.base.bean.EvaluateGoodsBean;

public class EvaluateFragment extends Fragment {
    private View view;
    private NoScrollGridView mainPullRefreshView;
    private EvaluateGoodsAdapter mAdapter;
    private List<RecommentBean> list;
    private String goodsId;

    public static EvaluateFragment getInstance(String goodsId) {
        EvaluateFragment fragment = new EvaluateFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", goodsId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        goodsId = bundle.getString("id");
        if (list == null) {
            list = new ArrayList<>();
        }
        list.clear();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
        view = inflater.inflate(R.layout.fragment_goods_evaluate, viewGroup, false);
        initView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    public void initView() {
        mainPullRefreshView = view.findViewById(R.id.mainPullRefreshView);
        mAdapter = new EvaluateGoodsAdapter(getActivity(), list);
        mainPullRefreshView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }

    public void initData() {
        JSONObject param = new JSONObject();
        try {
            param.put("goodsId", goodsId);
            param.put("limit", 30);
            param.put("page", 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiOkHttp.getCommentList(getActivity(), param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                list.clear();
                try {
                    JSONObject jsonObject = data.getJSONObject("data");
                    JSONArray records = jsonObject.getJSONArray("records");
                    for (int i = 0; i < records.length(); i++) {
                        RecommentBean bean = new Gson().fromJson(records.getJSONObject(i).toString(), RecommentBean.class);
                        list.add(bean);
                    }
                    mAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });
    }

}
